package org.hbbTlias2025.service.IMPL;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.hbbTlias2025.mapper.EmpExprMapper;
import org.hbbTlias2025.mapper.EmpMapper;
import org.hbbTlias2025.pojo.*;
import org.hbbTlias2025.service.EmpLogService;
import org.hbbTlias2025.service.EmpService;
import org.hbbTlias2025.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

//    private JwtUtil jwtUtil;

    @Override
    public pageReturn<Emp> findAll(EmpQueryParam empQueryParam) {

        try {
            PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());

            //2. 执行查询
            List<Emp> empList = empMapper.findAll(empQueryParam);

            //3. 解析查询结果, 并封装
            Page<Emp> p = (Page<Emp>) empList;
            return new pageReturn<>((int) p.getTotal(), p.getResult());
        } catch (Exception e){
            System.out.println(e);
            return null;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOne(Emp emp) throws Exception{
        int countEmpMapper;
        int countExpr;
        try {
            emp.setUpdateTime(LocalDateTime.now());
            emp.setCreateTime(LocalDateTime.now());
            List<EmpExpr> exprs = emp.getExprList();
            countEmpMapper = empMapper.addOne(emp);
            countExpr = 0;
            if(!CollectionUtils.isEmpty(exprs)){
                exprs.forEach(expr -> {
                    expr.setEmpId(emp.getId());
                    log.info("添加exprID: {}", emp.getId());
                });
                countExpr =empExprMapper.addALL(exprs);
            }
        } finally {
            empLogService.insertLog(new EmpLog(null, LocalDateTime.now(), "添加员工信息：" + emp.getName()));
        }

        return countEmpMapper + countExpr;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteIds(List<Integer> ids) {
        try {
            empMapper.deleteIds(ids);
            empExprMapper.deleteExprs(ids);
        } finally {
            empLogService.insertLog(new EmpLog(null, LocalDateTime.now(), "删除员工信息：" + ids));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Emp getOne(Integer id) {

        return empMapper.findOne(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateOne(Emp emp) {
        try {
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.updateOne(emp);

            empExprMapper.deleteExprs(Arrays.asList(emp.getId()));
            List<EmpExpr> exprs = emp.getExprList();
            if(!CollectionUtils.isEmpty(exprs)){
                exprs.forEach(expr -> {
                    expr.setEmpId(emp.getId());
//                    log.info("添加exprID: {}", emp.getId());

                });
                empExprMapper.addALL(exprs);
            }
        } finally {
            empLogService.insertLog(new EmpLog(null, LocalDateTime.now(), "修改员工信息" + emp.getId().toString()));
        }


//        根据员工id删除掉原有的工作经历，再更新为emp中新的工作经历

        return 0;
    }

    @Override
    public List<Emp> getAllEmpName() {

        return empMapper.getAllEmpName();

    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp e =  empMapper.selectByUserNameAndPassword(emp);

        if (e != null) {
            log.info("现在是登录成功了我去——" + e);

//            生成JWT令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId().toString());
            claims.put("username", e.getUsername());
            String JWT =  JwtUtil.generateToken(claims);
            log.info("JWT令牌我去————————: " + JWT);
            return new LoginInfo(e.getId(), e.getUsername(), e.getPassword(), JWT);
        }
        return null;
    }
}
